package com.othershe.nicedialog;

import android.content.Context;
import android.support.annotation.ColorRes;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.othershe.nicedialog.interf.DialogInterface;
import com.othershe.nicedialog.utils.DensityUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 模块说明：底部弹出框选择
 *
 * @author 陈浩
 * @date 2017/12/15
 */
public class SelectionDialog extends NiceDialog {
    private static Context mContext;
    List<String> datas = new ArrayList<>();
    private LinearLayout linearLayout;
    private int clickPosition;//最后一次选择的位置
    //标题属性
    private boolean boolTitle;
    private int titleHeight = 45;
    private String titleText = "请选择";
    private int titleTextColor = ContextCompat.getColor(mContext, R.color.toolbar_title_color);
    private float titleTextSize = 16;
    //item属性

    DialogInterface.OnItemClickListener<SelectionDialog> onItemClickListener;
    private int itemHeight = DensityUtils.dp2px(mContext, 44); // 默认item高度
    private float itemWidth = 1.0f;

    private int itemTextColor = ContextCompat.getColor(mContext, R.color.gray_location_text_color2);
    private float itemTextSize = 16;
    //取消按钮属性
    private String cancleButtonText = "取消";
    private TextView bottomBtn;

    @Override
    public int intLayoutId() {
        return R.layout.widget_bottom_dialog;
    }

    public static SelectionDialog newInstance(Context context) {
        mContext = context;
        SelectionDialog dialog = new SelectionDialog();
        dialog.setOutCancel(true);
        dialog.setShowBottom(true);
        return dialog;
    }

    private TextView getButton(String text, int position) {
        // 动态生成选择按钮
        final TextView button = new TextView(getContext());
        button.setText(text);
        button.setTag(position);
        button.setTextColor(titleTextColor);
        button.setTextSize(itemTextSize);
        button.setGravity(Gravity.CENTER);
        button.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams
                .MATCH_PARENT, itemHeight));
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (onItemClickListener != null) {
                    clickPosition = Integer.parseInt(button.getTag().toString());
                    onItemClickListener.onItemClick(SelectionDialog.this, button,
                            clickPosition);

                }
            }
        });

        return button;
    }

    public SelectionDialog setDatas(String[] data) {
        List<String> datas = Arrays.asList(data);
        this.datas = (datas == null ? new ArrayList<String>() : datas);

        return this;
    }

    public SelectionDialog setDatas(List<String> datas) {
        Log.i("xx", "setDatas");
        this.datas = (datas == null ? new ArrayList<String>() : datas);

        return this;
    }

    private void initData() {
        int count = linearLayout.getChildCount();
        if (count > 1) {
            linearLayout.removeViewsInLayout(1, count - 1);
        }
        //设置数据item
        if (datas.size() == 1) {
            TextView button = getButton(datas.get(0), 0);
//            if (mBuilder.getTitleVisible())
//                button.setBackgroundResource(R.drawable.selector_widget_actiondialog_bottom);
//            else button.setBackgroundResource(R.drawable.selector_widget_actiondialog_single);
            linearLayout.addView(button);

        } else if (datas.size() > 1) {
            for (int i = 0; i < datas.size(); i++) {
                TextView button = getButton(datas.get(i), i);
//                if (!mBuilder.getTitleVisible() && i == 0) {
//                    button.setBackgroundResource(R.drawable.selector_widget_actiondialog_top);
//                } else {
                if (i != datas.size() - 1)
                    button.setBackgroundResource(R.drawable
                            .selector_widget_actiondialog_middle_noraduis);
                else
                    button.setBackgroundResource(R.drawable
                            .selector_widget_actiondialog_bottom_noraduis);
//            }
                linearLayout.addView(button);
            }
        }
    }

    @Override
    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
//        Log.i("xx", "convertView");
//        super.convertView(holder, dialog);
        linearLayout = holder.getView(R.id.action_dialog_linearlayout);
        bottomBtn = holder.getView(R.id.action_dialog_botbtn);

        //设置底部“取消”按钮
        bottomBtn.setTextColor(itemTextColor);
        bottomBtn.setText(cancleButtonText);
        bottomBtn.setTextSize(itemTextSize);
        LinearLayout.LayoutParams btnlp = new LinearLayout.LayoutParams(AbsListView.LayoutParams
                .MATCH_PARENT, itemHeight);
        bottomBtn.setLayoutParams(btnlp);

        initData();
        //取消按钮
        holder.setOnClickListener(R.id.action_dialog_botbtn, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    public SelectionDialog setlTitleVisible(boolean isVisible) {
        this.boolTitle = isVisible;
        return this;
    }

    public SelectionDialog setTitleHeight(int dp) {
        this.titleHeight = dp;
        return this;
    }

    public int getFinalClickPosition() {

        return this.clickPosition;
    }

    public SelectionDialog setTitleText(String titleText) {
        this.titleText = titleText;
        return this;
    }


    public SelectionDialog setTitleTextColor(@ColorRes int titleTextColor) {
        this.titleTextColor = ContextCompat.getColor(mContext, titleTextColor);
        return this;
    }

    public SelectionDialog setTitleTextSize(int sp) {
        this.titleTextSize = sp;
        return this;
    }

    public SelectionDialog setItemHeight(int dp) {
        this.itemHeight = DensityUtils.dp2px(mContext, dp);
        return this;
    }


    public SelectionDialog setItemWidth(float itemWidth) {
        this.itemWidth = DensityUtils.dp2px(mContext, itemWidth);
        return this;
    }

    public SelectionDialog setItemTextColor(@ColorRes int itemTextColor) {

        this.itemTextColor = ContextCompat.getColor(mContext, itemTextColor);
        return this;
    }

    public SelectionDialog setItemTextSize(int sp) {
        this.itemTextSize = sp;
        return this;
    }

    public SelectionDialog setCancleButtonText(String cancleButtonText) {
        this.cancleButtonText = cancleButtonText;
        return this;
    }

    @Override
    public SelectionDialog setOutCancel(boolean outCancel) {
        super.setOutCancel(outCancel);
        return this;
    }

    @Override
    public SelectionDialog setShowBottom(boolean showBottom) {
        super.setShowBottom(showBottom);
        return this;
    }

    @Override
    public SelectionDialog show(FragmentManager manager) {
        super.show(manager);
        return this;
    }

    public SelectionDialog setOnItemClickListener(DialogInterface.OnItemClickListener<SelectionDialog> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        return  this;
    }

}
